﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.WS.UserControls;
using GCELibrary.Current;
using System.Transactions;
using ZF.BLL;
namespace ZF.WS.Admin
{
    public partial class MemberRight : ZF.BLL.Page.MMPage
    {
        [PageArgument(Key = "mid", CanNull = false)]
        public int MemberId { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!IsPostBack)
            {
    
                    Edit();
            }
        }
        public void InitControls()
        {
            //this.cblBaseFacility.DataSource = DictionaryConfig.HBaseFacility;
            //this.cblBaseFacility.DataBind();
            //this.cblWithFacility.DataSource = DictionaryConfig.HWithFacility;
            //this.cblWithFacility.DataBind();
            //this.rblDecoration.DataSource = DictionaryConfig.HDecoration;
            //this.rblDecoration.DataBind();
            //this.ddlDirection.DataSource = DictionaryConfig.HDirection;
            //this.ddlDirection.DataBind();
            //this.ddlHouseType.DataSource = DictionaryConfig.HHouseType;
            //this.ddlHouseType.DataBind();
            //this.ddlProperty.DataSource = DictionaryConfig.HProperty;
            //this.ddlProperty.DataBind();

            //var dt = HouseEditPage.DealType.ToString();
            //foreach (Control c in this.Controls)
            //{
            //    if (c is HtmlControl)
            //    {
            //        var hc = (HtmlControl)c;
            //        if (!string.IsNullOrEmpty(hc.Attributes["key"]))
            //        {
            //            hc.Visible = hc.Attributes["key"].Split(',').Contains(dt);
            //        }

            //    }
            //}
            //if (HouseEditPage.DealType == 2)
            //{
            //    this.cbHZ.Checked = true;
            //}
            //HouseEditPage.Type = (HouseEditPage.OperType == PageOperType.AddNew ? "发布" : "修改") + SiteInfo.GetHouseTitle(HouseEditPage.DealType);

        }

        public override ModuleType Module
        {
            get
            {
                return ModuleType.SysAdmin;
            }
            protected set
            {
                base.Module = value;
            }
        }
 

        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                var admin = dc.US_Member.FirstOrDefault(p => p.MemberId == MemberId && p.Type == 100); ;
                if (admin == null)
                    throw WarnFactory.CreateWarn(WarnCode.ParameterError);
  
                InitControls(); ;
                List<ModuleRight> moduleRights = new List<ModuleRight>();
                var hr = admin.Right.EmptyString("").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            
                foreach (ModuleType type in Enum.GetValues(typeof(ModuleType)))
                {
                    if (type != ModuleType.None)
                    {
                        var des = DescriptionAttribute.GetEnumDescription(type);
                        moduleRights.Add(new ModuleRight()
                        {
                            Description = des.Description,
                            IsRight = hr.Contains(type.ToString()),
                            ModuleName = des.Name,
                            Type = type,
                        });
                    }
                }
                this.lvModule.DataSource = moduleRights;
                this.lvModule.DataBind();




 




            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {
                    var admin = dc.US_Member.FirstOrDefault(p => p.MemberId == MemberId && p.Type == 100); ;
                    if (admin == null)
                        throw WarnFactory.CreateWarn(WarnCode.ParameterError);

                    admin.Right = this.tbRight.Text;




                    try
                    {
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }

                    WarnFactory.CreateWarn(new PageWarnParameter()
                    {
                        JscriptMode = true,
                        Link = WarnLink.Current,
                        Message = "操作成功",

                    }).Alert();
                     




                }


            }

        }

 
    }
    class ModuleRight
    {
        public string ModuleName { get; set; }
        public string Description { get; set; }
        public ModuleType Type { get; set; }
        public bool IsRight { get; set; }
    }
  
}
